#define _CRT_SECURE_NO_WARNINGS 1
include <bits / stdc++.h>
using namespace std;
int a[10005];
int main()
{
    int n;cin >> n;int ans = 0;
    for (int i = 1;i <= n;i++) { cin >> a[i];ans += a[i]; }
    sort(a + 1, a + 1 + n);
    int k = 1;
    while (a[k] == a[1]) {
        k++;
    }
    int m = n;
    while (a[m] == a[n]) {
        m--;
    }
    ans -= (a[1] * (k - 1) + a[n] * (n - m));
    cout << ans;
    return 0;
}